const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    input_list: [],
    add_url:'',
    index_url:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.showLoading({
      title: '加载中…',
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/getAddInfo?id=' + options.cid + '&token=' + this.data.token + "&infoid=" + options.id,
      success: res => {
        var data = res.data;
        wx.setNavigationBarTitle({
          title: '发布-'+data.info.title,
        })
        // var form = data.form;
        that.setData({
          input_list: data.form,
          add_url: data.add_url + '&token=' + that.data.token,
          index_url: data.index_url
        })
        wx.hideLoading();
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  setTags: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;

    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].tags[data.index].selected = !list[idx].tags[data.index].selected;
      }
    })
    this.setData({
      input_list: list
    })
  },
  toggleTagsInput: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].is_show_tags_input = true;
      }
    })
    this.setData({
      input_list: list
    })
  },
  addTags: function (e) {
    var data = e.currentTarget.dataset;
    var value = e.detail.value;
    if(value.length > 4){
      wx.showToast({
        title: '标签长度不能超过4个字',
      })
      return false;
    }
    var tag = { title: value, selected: true };
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].is_show_tags_input = false;
        list[idx].tags_input_value = '';
        if (value !== '') {
          list[idx].tags.push(tag);
        }
      }
    })
    this.setData({
      input_list: list
    })
  },
  bindChange: function (e) {
    // console.log(e);
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].value = e.detail.value;
      }
    })
    this.setData({
      input_list: list
    })
  },
  bindUploadImg: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    var that = this;
    wx.chooseImage({
      count: data.num > 1 ? 9 : 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中……',
        })
        tempFilePaths.forEach(function (item, idx) {
          wx.uploadFile({
            url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + that.data.token,
            filePath: item,
            name: 'file',
            success: function (res) {
              var out = JSON.parse(res.data);
              console.log(out);
              if (out.err_code > 0) {
                wx.showToast({
                  title: '上传失败！',
                })
              } else {
                list.forEach(function (value, index) {
                  console.log(value);
                  console.log(data);
                  if (value.title == data.title) {
                    var pics = list[index].pics;
                    var file_name = out.data.domain;
                    if (data.num > 1) {
                      pics.push(file_name);
                    } else {
                      pics = [file_name];
                    }
                    list[index].pics = pics;
                  }
                })
                that.setData({
                  input_list: list
                })
              }

              if (idx == tempFilePaths.length - 1) {
                wx.hideLoading();
              }
              //do something
            }
          })
        })
      }
    })
  },
  formSubmit: function (e) {
    // console.log(e.detail.value);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    console.log(data)
    data.input_list.forEach(function (item, idx) {
      if (item.type == 'picker1') {
        post_data[item.name] = item.range[item.value];
      } else if (item.type == 'picker2') {
        post_data[item.name[0]] = item.range[0][item.value[0]];
        post_data[item.name[1]] = item.range[1][item.value[1]];
      } else if (item.type == 'date') {
        post_data[item.name] = item.value;
      } else if (item.type == 'time') {
        post_data[item.name] = item.value;
      } else if (item.type == 'tag') {
        var tags = '';
        var i = 0;
        item.tags.forEach(function (vo, key) {
          // console.log(vo);
          if (vo.selected){
            if (i == 0) {
              tags += vo.title;
            } else {
              tags += ',' + vo.title;
            }
            i++;
          }
        })
        post_data[item.name] = tags;
      } else if (item.type == 'pic') {
        post_data[item.name] = item.pics;
        post_data.imgurl = item.pics[0];
      }
    })
    post_data.openid = app.globalData.openid;
    
    wx.request({
      url: data.add_url,
      method: 'POST',
      data: post_data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.err_code === 400) {
          wx.showToast({
            title: res.data.err_msg,
          })
        } 

      }
    })
  },
})